﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Weazel.Math;

#endregion

namespace Weazel.Math.Test
{
  [TestClass]
  public class TestAssignment
  {
    [TestMethod]
    public void AssignLong_1()
    {
      Integer a = new Integer(64, false);
      a.Assign((long)123456789101112);
      Assert.AreEqual("123456789101112", a.ToDecimalString());
    }

    [TestMethod]
    public void AssignLong_2()
    {
      Integer a = new Integer(64, true);
      a.Assign((long) -1);
      Assert.AreEqual("ffffffffffffffff", a.ToHexadecimalString());
    }

    [TestMethod]
    public void AssignLong_3()
    {
      Integer a = new Integer(40, true);
      a.Assign((long)-1);
      Assert.AreEqual("ffffffffff", a.ToHexadecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsignedOverflow()
    {
      Integer a = new Integer(8, "0xFF", false);
     
      try
      {
        a.AssignHexadecimalString("0xFFFF");
        Assert.Fail("Expected WidthMismatchException");
      }
      catch (WidthMismatchException)
      {
      }
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned1()
    {
      Integer a = new Integer(8, "0xFF", false);
      Assert.AreEqual((uint)0x000000FF, (uint)a.Content[0]);
      Assert.AreEqual(1, a.Content.Length);
      Assert.AreEqual(false, a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned2()
    {
      Integer a = new Integer(16, "0xFFF", false);
      Assert.AreEqual((uint)0x00000FFF, (uint)a.Content[0]);
      Assert.AreEqual(1, a.Content.Length);
      Assert.AreEqual(false, a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned3()
    {
      Integer a = new Integer(42, "0xAAAAAAAAAAA", false);
      Assert.AreEqual((uint)0xAAAAAAAA, (uint)a.Content[0]);
      Assert.AreEqual((uint)0x000002AA, (uint)a.Content[1]);
      Assert.AreEqual(2, a.Content.Length);
      Assert.AreEqual(false, a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned4()
    {
      Integer a = new Integer(64, "0x5555555555555555", false);
      Assert.AreEqual(a.Content[0], (uint)0x55555555);
      Assert.AreEqual(a.Content[1], (uint)0x55555555);
      Assert.AreEqual(a.Width, 64);
      Assert.IsFalse(a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned5()
    {
      Integer i = new Integer(8, false);
      i.AssignHexadecimalString("0xFF");
      Assert.AreEqual("255", i.ToDecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned6()
    {
      Integer i = new Integer(8, false);
      i.AssignHexadecimalString("0x7F");
      Assert.AreEqual("127", i.ToDecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned7()
    {
      Integer i = new Integer(64, false);
      i.AssignHexadecimalString("0x7FFFFFFFFFFFFFFF");
      Assert.AreEqual("7fffffffffffffff", i.ToHexadecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentUnsigned8()
    {
      Integer i = new Integer(64, false);
      i.AssignHexadecimalString("0x8000000000000000");
      Assert.AreEqual("8000000000000000", i.ToHexadecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned1()
    {
      Integer a = new Integer(8, "0xFF", true);
      Assert.AreEqual((uint)0xFFFFFFFF, a.Content[0]);
      Assert.AreEqual(1, a.Content.Length);
      Assert.AreEqual(a.Width, 8);
      Assert.IsTrue(a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned2()
    {
      Integer a = new Integer(16, "0x0FFF", true);
      Assert.AreEqual(a.Content[0], (uint)0x00000FFF);
      Assert.AreEqual(1, a.Content.Length);
      Assert.AreEqual(a.Width, 16);
      Assert.IsTrue(a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned3()
    {
      Integer a = new Integer(64, "0x5555555555555555", true);
      Assert.AreEqual(a.Content[0], (uint)0x55555555);
      Assert.AreEqual(a.Content[1], (uint)0x55555555);
      Assert.AreEqual(a.Width, 64);
      Assert.IsTrue(a.Signed);
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned_4()
    {
      Integer i = new Integer(8, true);
      i.AssignHexadecimalString("0xFF");
      Assert.AreEqual("-1", i.ToDecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned_5()
    {
      Integer i = new Integer(8, true);
      i.AssignHexadecimalString("0x7F");
      Assert.AreEqual("127", i.ToDecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned_6()
    {
      Integer i = new Integer(64, true);
      i.AssignHexadecimalString("0x7FFFFFFFFFFFFFFF");
      Assert.AreEqual("7fffffffffffffff", i.ToHexadecimalString());
    }

    [TestMethod]
    public void HexadecimalAssignmentSigned_7()
    {
      Integer i = new Integer(64, true);
      i.AssignHexadecimalString("0x8000000000000000");
      Assert.AreEqual("8000000000000000", i.ToHexadecimalString());
    }

    /**************************************************************************
		 * AssignBinaryString(string)
		 **************************************************************************/

    /**************************************************************************
		 * AssignBinaryString(string)
		 **************************************************************************/
    [TestMethod]
    public void TestAssignBinaryStringUnsigned_1()
    {
      Integer i = new Integer(8, false);
      i.AssignBinaryString("0b0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringUnsigned_2()
    {
      Integer i = new Integer(96, false);
      i.AssignBinaryString("0b0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringUnsigned_3()
    {
      Integer i = new Integer(8, false);
      i.AssignBinaryString("0b11111111");
      Assert.AreEqual("255", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringUnsigned_4()
    {
      Integer i = new Integer(64, false);
      i.AssignBinaryString("0b1111111111111111111111111111111111111111111111111111111111111111");
      Assert.AreEqual("ffffffffffffffff", i.ToHexadecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringUnsigned_5()
    {
      Integer i = new Integer(8, false);
      i.AssignBinaryString("0b01111111");
      Assert.AreEqual("127", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringUnsigned_6()
    {
      Integer i = new Integer(8, false);
      i.AssignBinaryString("0b10000000");
      Assert.AreEqual("128", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_1()
    {
      Integer i = new Integer(8, true);
      i.AssignBinaryString("0b0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_2()
    {
      Integer i = new Integer(96, true);
      i.AssignBinaryString("0b0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_3()
    {
      Integer i = new Integer(8, true);
      i.AssignBinaryString("0b11111111");
      Assert.AreEqual("-1", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_4()
    {
      Integer i = new Integer(34, true);
      i.AssignBinaryString("0b1111111111111111111111111111111111");
      Assert.AreEqual("-1", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_5()
    {
      Integer i = new Integer(8, true);
      i.AssignBinaryString("0b01111111");
      Assert.AreEqual("127", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryStringSigned_6()
    {
      Integer i = new Integer(8, true);
      i.AssignBinaryString("0b10000000");
      Assert.AreEqual("-128", i.ToDecimalString());
    }

    /**************************************************************************
		 * AssignDecimalString(string)
		 **************************************************************************/

    [TestMethod]
    public void TestAssignDecimalStringInvalidArgument_1()
    {
      Integer i = new Integer(8, false);
      try
      {
        i.AssignDecimalString("NOTADECIMALSTRING");
      }
      catch (ArgumentException)
      {
        return;
      }

      Assert.Fail();
    }

    [TestMethod]
    public void TestAssignDecimalStringUnsigned_1()
    {
      Integer i = new Integer(8, false);
      i.AssignDecimalString("0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringUnsigned_2()
    {
      Integer i = new Integer(8, false);
      i.AssignDecimalString("10");
      Assert.AreEqual("10", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringUnsigned_3()
    {
      Integer i = new Integer(8, false);
      i.AssignDecimalString("255");
      Assert.AreEqual("255", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringUnsigned_4()
    {
      Integer i = new Integer(64, false);
      i.AssignDecimalString("12345678901212");
      Assert.AreEqual("12345678901212", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_1()
    {
      Integer i = new Integer(8, true);
      i.AssignDecimalString("0");
      Assert.AreEqual("0", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_2()
    {
      Integer i = new Integer(8, true);
      i.AssignDecimalString("10");
      Assert.AreEqual("10", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_3()
    {
      Integer i = new Integer(8, true);
      i.AssignDecimalString("-10");
      Assert.AreEqual("-10", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_4()
    {
      Integer i = new Integer(64, true);
      i.AssignDecimalString("12345678901212");
      Assert.AreEqual("12345678901212", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_5()
    {
      Integer i = new Integer(64, true);
      i.AssignDecimalString("-12345678901212");
      Assert.AreEqual("-12345678901212", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignDecimalStringSigned_6()
    {
      Integer i = new Integer(64, true);
      i.AssignDecimalString("-12345678910111213");
      Assert.AreEqual("-12345678910111213", i.ToDecimalString());
    }

    [TestMethod]
    public void TestAssignBinaryString_1()
    {
      Integer i = new Integer(16, false);
      i.AssignBinaryString("1111111100000000");
      Assert.AreEqual("ff00", i.ToHexadecimalString());
    }
  }
}
